//
//  WSWebController.m
//  CharonTVProject
//
//  Created by karong on 2023/7/5.
//

#import "WSWebController.h"
#import <WebKit/WKWebView.h>
#import <WebKit/WKPreferences.h>
#import <WebKit/WKWebViewConfiguration.h>

@interface WSWebController () <UIGestureRecognizerDelegate,WKUIDelegate,WKNavigationDelegate>
@property (weak, nonatomic) IBOutlet UIView *nav;
@property (assign, nonatomic) BOOL urlChange;
@property (strong, nonatomic) WKWebView *webView;
@property (strong, nonatomic) WKWebViewConfiguration *config;
@property (assign, nonatomic) BOOL canBack;
@property (assign, nonatomic) BOOL hiddenHud;

@end

@implementation WSWebController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    self.view.backgroundColor = UIColor.whiteColor;
    [self addWeb];
    self.title = self.titleStr;
}


- (void)addWeb{
    self.config = [[WKWebViewConfiguration alloc] init];
    self.config.preferences.javaScriptEnabled = YES;
    self.config.preferences.javaScriptCanOpenWindowsAutomatically = YES;
    self.webView = [[WKWebView alloc]initWithFrame:CGRectZero configuration:self.config];
    self.webView.backgroundColor = self.view.backgroundColor;
    self.webView.allowsBackForwardNavigationGestures = NO;
    self.webView.scrollView.showsVerticalScrollIndicator = NO;
    self.webView.scrollView.showsHorizontalScrollIndicator = NO;
    self.webView.UIDelegate = self;
    self.webView.navigationDelegate = self;
    [self.view addSubview:self.webView];
    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.nav.mas_bottom);
        make.left.right.bottom.mas_equalTo(self.view);
    }];
    NSString * string = FULL_ADDRESS([WSTools fullWebUrl:self.url]);
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:[string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
    [self.webView loadRequest:request];
}

- (void)clickBackBtn{
    if ([self.webView canGoBack] && !self.canBack) {
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        if ([self.webView canGoBack]) {
            [self.webView goBack];
        }else{
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
}

#pragma mark
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
    if (!self.hiddenHud) {
        self.webView.hidden = YES;
    }
    self.hiddenHud = NO;
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    /// 延时0.2s 显示网页
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self showWebView];
    });
}

- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error{
    self.webView.hidden = NO;
}

- (void)showWebView{
    self.webView.hidden = NO;

    for (UIView* subview in self.webView.subviews) {
        for (UIGestureRecognizer* longPress in subview.gestureRecognizers) {
            if ([longPress isKindOfClass:UILongPressGestureRecognizer.class]) {
                [subview removeGestureRecognizer:longPress];
            }
        }
    }
}

@end
